package com.kadron.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import com.kadron.entity.Application;

@AutoCreate
@BypassInterceptors
@Name("applicationDAO")
@Scope(ScopeType.EVENT)
public class KApplicationDAO extends KDAO<Application> {

	public Application getNew() {
		EntityManager em = getEntityManager();
		try {
			return (Application) em.createQuery("SELECT e FROM Application e ORDER BY e.id DESC")
			.setMaxResults(1)
			.getSingleResult();			
		} catch (NoResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Long> getApplications(String name) {
		EntityManager em = getEntityManager();
		return em.createQuery("SELECT e.id FROM Application e WHERE (e.name_en LIKE :name OR e.name_sr LIKE :name OR e.name_es LIKE :name) ORDER BY e.id DESC")
			.setParameter("name", name+"%")
			.getResultList();			
	}
	
}
